package com.j170502.cateringmange.controller;

import com.j170502.cateringmange.entity.Role;
import com.j170502.cateringmange.entity.User;
import com.j170502.cateringmange.service.RoleService;
import com.j170502.cateringmange.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @RequestMapping("/")
    public String start(Model model){
       String [] names = userService.findAllName();
       model.addAttribute("names",names);
        return "index";
    }
    @RequestMapping("/login")
    public String login(HttpSession session, String userName, String userPwd, Model model){
       User user= userService.login(userName,userPwd);
       if(user !=null){
          session.setAttribute("user",user);
           System.out.println("登录成功");

           return "redirect:/first";
       }else{
          model.addAttribute("msg","账号或密码错误!");
           String [] names = userService.findAllName();
           model.addAttribute("names",names);
           return "index";
       }
    }
    @RequestMapping("/user/list")
    public String list(Model model){
        List<User> listUser=userService.findAlls();
        List<Role> listRole=roleService.findAll();
        model.addAttribute("listUser",listUser);
        model.addAttribute("listRole",listRole);
        return "setting/user/list";
    }

}
